define(['app'],function(app){
    app.controller('BookNoteCtrl' ,['$scope','$state','$rootScope','$location', '$timeout', '$http','$q','nodeService',
       function ($scope,$state, $rootScope, $location,$timeout,$http, $q,nodeService) {
        
        //代码书写步骤
        //1.用【$scope】定义公共类model,和页面绑定的model
        //2.用【var】定义controller内部用的变量
        //3.用【$scope】和【var】定义方法，注意区分两者定义的方法的区别
        //4.最后定义【init】初始化方法，并调用，进行页面的初始化加载
        //定义规则，按可用性分忧优先级，公共的在前，private的在后

           //阅读笔记集合
           $scope.bookNoteData = [];
           //查询条件
           $scope.filter={
               name:'',
               author:''
           }
           //每页显示条数
           $scope.params=[{num:3,select:true},{num:6,select:false},{num:9,select:false},{num:12,select:false}];
           $scope.page = {
               nowPage: 1,
               pageCount: 10,
               allCount: 0,
               NumOfPage: 0
           }
           //查询方法
           $scope.search=function(){
               nodeService.getNodeList($scope.page.nowPage,$scope.page.pageCount,$scope.filter.name,$scope.filter.author).then(
                   function (response) {
                       if(response.status==200)
                       {
                           $scope.bookNoteData = response.data.list;
                           $scope.page.allCount=response.data.total;
                           $scope.page.NumOfPage=response.data.pages;
                       }
                       else {
                           alert("查询数据失败!");
                       }
                   },
                   function (error) {
                       alert("查询数据失败！");
                   }
               );
           }
           $scope.countChanged=function(count){
               $scope.params.forEach(function(item,index,arr){
                   if(item.num==count.num)
                   item.select=true;
                   else
                   item.select=false;
               })
               $scope.page.pageCount=count.num;
               var number = parseInt($scope.page.allCount / $scope.page.pageCount);
               if (parseInt($scope.page.allCount % $scope.page.pageCount) != 0)
                   number += 1;
               $scope.page.NumOfPage = number;
               $scope.search();
           }

           $scope.countChanged($scope.params[0]);
           //分页
           $scope.pageChanged = function (index) {
                   switch (index) {
                       case 1:
                           if ($scope.page.nowPage > 1)
                               $scope.page.nowPage -= 1;
                           break;
                       case 2:
                           if ($scope.page.NumOfPage - $scope.page.nowPage > 0)
                               $scope.page.nowPage += 1;
                           break;
                   }
               $scope.search();
           }

           //添加编辑方法
           $scope.addNote=function(item,type){
                //调用添加方法
                if(type==1)
                {
                        $state.go("book.note.edit");
                }
                //编辑和查看
               else
               {
                   $state.go("book.note.edit",{ 'id': item.id,'type':type });
               }
           }
           //删除方法
           $scope.deleteNote = function (item) {
               if (!confirm("确认删除吗？")) {
                   return;
               }
               nodeService.deleteNode(item.id).then(
                   function (response) {
                       if(response.status==200)
                       {
                           alert("删除成功！");
                           $scope.search();
                       }
                       else
                           alert("删除失败！");
                   },
                   function (error) {
                       alert("删除失败！");
                   }
               );
           }

        var init = function () {
            console.log("BookNoteCtrl init");
            $scope.dashboardModel.isActiveMenu = 'note';//左侧菜单按钮赋初值
        };
        init();
    }]);
});